#include <time.h>
#include <math.h>
#include <stdlib.h> 
#include <time.h>  
#include <stdio.h>
#include <pthread.h>
#define NUM_THREADS	3
#define SIZE	500
int *task[NUM_THREADS];
int array[SIZE];
void randomfill(int array[SIZE]);
//mutex_Lock LOCK=PTHREAD_MUTEX_INITIALIZER;

/////////////////////////////////////////////////////////////////////////////////////
void randomfill(int array[SIZE])
{
 srand((unsigned)time(0)); 
  for(int i=0; i<SIZE; i++) 
        array[i] = (rand()%1000)+1;
}
///////////////////////////////////////////////////////////////////
void *min(int array[SIZE])
{
int mini=array[0];
for (int i=1;i<SIZE;i++)
if(mini>array[i])
mini=array[i];
printf("\nMinimum value in the arrray is : %d:",mini);
// return mini;
}
////////////////////////////////////////////////////////////////////////
void *max(int array[SIZE])
{
int maxi=array[0];
for (int i=1;i<=SIZE;i++)
if(maxi<array[i])
maxi=array[i];
printf("\nMaximum value in the arrray is : %d:",maxi);
//return maxi;
}
/////////////////////////////////////////////////////////////////////////////////

void *mean(int array[SIZE])
{
int mea=0,mea1=0;
for (int i=0;i<SIZE;i++)
mea=mea+array[i];
mea1=mea/500;
printf("\nMean value of the arrray is :  %d \n",mea1);
//return mea1;
}
////////////////////////////////////////////////////////////////////////////////////
void *RunThread(void *threadid)
{
int *id_ptr, taskid;
void *status;
//pthread_mutex_init();
//mutex_Lock(&LOCK);
   //sleep(4);
   id_ptr = (int *) threadid;
   taskid = *id_ptr;
printf("Thread %d: %d\n", taskid, task[taskid]);
//mutex_Unlock(&LOCK);
pthread_join(task[0],&status);
pthread_join(task[1],&status);
pthread_join(task[2],&status);
pthread_exit(NULL); 

}
///////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
printf("Starting from the first line of the main function:\n");
clock_t tic = clock();
pthread_t threads[NUM_THREADS];
int *taskids[NUM_THREADS];
int rc, t;
//int* array[SIZE];
randomfill(&array);
  //printf("Randomly Generated array(500 integers) in between 0 and 1000 is \n");
  //for(int i=0; i<SIZE; i++)
    //printf( "%d   ",array[i]);
task[0] =min(array);
task[1] =max(array);
task[2] =mean(array);

for(t=0;t<NUM_THREADS;t++)
 {
  taskids[t] = (int *) malloc(sizeof(int));
  *taskids[t] = t;
  printf("Creating thread %d\n", t);
  rc = pthread_create(&threads[t], NULL, RunThread, (void *) taskids[t]);
  if (rc)
 {
    printf("ERROR; return code from pthread_create() is %d\n", rc);
    exit(-1);
  }
 pthread_exit(NULL); 
}

clock_t toc = clock();
printf("\nAnd Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
}

